package freespiritsocial.io.user.base

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.pm.PackageInfo
import android.os.Build
import android.view.Gravity.CENTER
import androidx.camera.camera2.Camera2Config
import androidx.camera.core.CameraXConfig
import com.kingja.loadsir.core.LoadSir
import com.lishuaihua.toast.ToastUtils
import com.previewlibrary.ZoomMediaLoader
import com.sendbird.android.SendBird
import freespiritsocial.io.user.R
import freespiritsocial.io.user.fcm.MyFirebaseMessagingService
import freespiritsocial.io.user.util.AppUncaughtExceptionHandler
import freespiritsocial.io.user.util.PushUtils
import freespiritsocial.io.user.util.TestImageLoader
import freespiritsocial.io.user.weight.loadCallback.ErrorCallback
import freespiritsocial.io.user.weight.loadCallback.LoadingCallback
import me.hgj.jetpackmvvm.base.BaseApp


class App: BaseApp(){


    companion object {
        lateinit var instance: App
    }

    override fun onCreate() {
        super.onCreate()
        AppUncaughtExceptionHandler().init(this)

        instance = this
        ToastUtils.init(this)
        ToastUtils.setGravity(CENTER,0,0)
        ZoomMediaLoader.getInstance().init(TestImageLoader())

        val channelId = getString(R.string.default_notification_channel_id)

        val notificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Create channel to show notifications.
            val channelName = getString(R.string.default_notification_channel_id)
            notificationManager.createNotificationChannel(
                NotificationChannel(
                    channelId,
                    channelName,NotificationManager.IMPORTANCE_HIGH
                )
            )
        }

        //界面加载管理 初始化
        LoadSir.beginBuilder()
            .addCallback(LoadingCallback())//加载
            .addCallback(ErrorCallback())//错误
            .setDefaultCallback(LoadingCallback::class.java)//设置默认加载状态页
            .commit()


        SendBird.init(getString(R.string.chat_id),this)

        PushUtils.registerPushHandler(MyFirebaseMessagingService())
        /* Bugly */
        /* CrashReport | CrashReportInfo*/
//        CrashReport.setIsDevelopmentDevice(this, BuildConfig.DEBUG)
//        val strategy = UserStrategy(this)
//        strategy.appChannel = "Google"
//        strategy.appVersion = (if(BuildConfig.DEBUG) "DEBUG" else "RELEASE") + " in " + BuildConfig.VERSION_NAME + " - " +BuildConfig.VERSION_CODE
//        CrashReport.initCrashReport(applicationContext,"8ee335cd05",BuildConfig.DEBUG,strategy)
//        System.currentTimeMillis().getTimeFormat()
    }

    fun isAvilible(packageName: String): Boolean {
        val packageNames = ArrayList<String>()
        this.packageManager?.getInstalledPackages(0)?.let {
            for (info: PackageInfo in it) {
                packageNames.add(info.packageName)
            }
        }
        return packageNames.contains(packageName)
    }

}
